package com.youfan.control;

import com.youfan.entity.ProductType;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "productType")
public class ProducttypeControl {

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestBody ProductType youfanProductType)
    {
        try {
            int sucessnums = productTypeService.insertOne(youfanProductType);
            return Result.success(sucessnums,"添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody ProductType youfanProductType){
        try {
            if (youfanProductType.getPageNum() == null) {
                youfanProductType.setPageNum(1);
            }
            if (youfanProductType.getPageSize() == null) {
                youfanProductType.setPageSize(5);
            }
            CommonPage<ProductType> resultdata = productTypeService.queryByCondtion(youfanProductType);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "view")
    @ResponseBody
    public Result view(@RequestBody ProductType youfanProductType)
    {
        try {
            ProductType youfanProductType1 = productTypeService.findByid(youfanProductType);
            return Result.success(youfanProductType1,"查询单个对象成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询单个对象失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestBody ProductType youfanProductType)
    {
        try {

            ProductType youfanProductType1 =  productTypeService.findByid(youfanProductType);
            youfanProductType1.setName(youfanProductType.getName());
            youfanProductType1.setProductypeDesc(youfanProductType.getProductypeDesc());
            youfanProductType1.setNavStatus(youfanProductType.getNavStatus());
            youfanProductType1.setShowStatus(youfanProductType.getShowStatus());
            youfanProductType1.setParentid(youfanProductType.getParentid());
            youfanProductType1.setLevel(youfanProductType.getLevel());
            int result = productTypeService.updateOne(youfanProductType1);
            return Result.success(result,"更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }


}
